03 / 04

What is the difference between a nil interface and an interface holding a nil pointer?

A nil interface has no type and no value. An interface holding a nil pointer has a concrete type but a nil pointer value — this interface is NOT nil, which is a classic Go gotcha in error handling.

The nil interface trap
Rules to remember
  1. 1

    An interface value is nil only when both its type and value fields are nil

  2. 2

    Assigning a typed nil pointer to an interface creates a non-nil interface

  3. 3

    Always return untyped nil (return nil) for error return values

  4. 4

    Use errors.As() to inspect error types rather than direct type assertions on possibly-nil errors

  5. 5

    This is one of the most common subtle bugs in Go code — affects all interface types, not just error